package com.samsung.actividades;

import java.util.Vector;

import com.samsung.R;
import com.samsung.database.Orden;
import com.samsung.database.Parte;
import com.samsung.database.PartesDBAdapter;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;




public class Resumen extends Activity implements OnClickListener{
    ListView resumen;
    Button seleccionar;
    Orden orden;
    Orden new_orden;
    private PartesDBAdapter partesDBAdapter;
    private Cursor partesCursor;
    
    Logs log;
    
    String ruta;	
	String ruta_parte="";
    
//	private static final String[] GENRES = new String[] { "0 Action",
//			"1 Adventure", "2 Animation", "3 Children"
//
//	};


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        
        partesDBAdapter = new PartesDBAdapter(this);
		partesDBAdapter.open();
		
		
		
		partesCursor = partesDBAdapter.getRuta();
		startManagingCursor(partesCursor);	
		
		if(partesCursor.moveToFirst()){
			ruta_parte = partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_VALOR));					
		}
		
               
        setContentView(R.layout.resumenlayout);
        
        Intent i = getIntent();
		orden = (Orden) i.getParcelableExtra(Inicio.KEY_ORDEN);
        
		       
        Resources res = getResources();
        
        TextView modeloText = (TextView) findViewById(R.id.textmodelo);
        modeloText.setText("Modelo: "+orden.getModelo());
        

		Button menuRep = (Button) findViewById(R.id.guardar);
		menuRep.setBackgroundDrawable(res.getDrawable(R.drawable.guardar));
		menuRep.setOnClickListener(this);

		resumen = (ListView) findViewById(R.id.listaPartes);
		
			
		Vector<Parte> partes=orden.getPartes();
		int tam=partes.size();
		
		String[] TXT_PARTES=new String[tam];
		
		for(int a=0;a<tam;a++){
			TXT_PARTES[a]=partes.get(a).toString();			
		}
		
		//resumen.setAdapter(new ArrayAdapter<String>(this,
		//		android.R.layout.simple_list_item_multiple_choice, TXT_PARTES));
		resumen.setAdapter(new ArrayAdapter<String>(this,
				R.layout.lista_copia, TXT_PARTES));
		
		resumen.setItemsCanFocus(false);
		resumen.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		seleccionar = (Button) findViewById(R.id.guardar);
		new_orden=new Orden();
		
//		seleccionar.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View view) {
//
//				SparseBooleanArray ids = resumen.getCheckedItemPositions();
//				System.out.println("---------------------------------------");
//				for (int i = 0; i < ids.size(); i++) {
//					int key = ids.keyAt(i);
//					// if(ids.valueAt(i)){
//					// String value = (String)resumen.gettemIdAtPosition(i);
//					String value = (String) resumen.getItemAtPosition(key);
//					System.out.println("key " + i + "  " + value + "  "
//							+ ids.valueAt(i));
//					if(ids.valueAt(i)){
//						new_orden.addParte(orden.getParte(i));
//					}
//					// }
//				}
//				/*
//				 * long[] lg=resumen.getCheckedItemIds();
//				 * System.out.println("---------------------------------------"
//				 * ); for (int i = 0; i < lg.length; i++) {
//				 * System.out.println("value "+i+"  "+lg[i]);
//				 * 
//				 * }
//				 */
//				resumen.clearChoices();
//				SendSms sms = new SendSms(this);
//			    sms.sendSMS(new_orden);
//
//			}
//		});
	}

	

    public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.guardar:
			SparseBooleanArray ids = resumen.getCheckedItemPositions();
			System.out.println("---------------------------------------");
			for (int i = 0; i < ids.size(); i++) {
				int key = ids.keyAt(i);
				// if(ids.valueAt(i)){
				// String value = (String)resumen.gettemIdAtPosition(i);
				String value = (String) resumen.getItemAtPosition(key);
				System.out.println("key " + i + "  " + value + "  "
						+ ids.valueAt(i));
				if(ids.valueAt(i)){
					new_orden.addParte(orden.getParte(i));
				}
				new_orden.setEstado(orden.getEstado());
				new_orden.setNumero(orden.getNumero());
				new_orden.setSintoma(orden.getSintoma());
				// }
			}
			/*
			 * long[] lg=resumen.getCheckedItemIds();
			 * System.out.println("---------------------------------------"
			 * ); for (int i = 0; i < lg.length; i++) {
			 * System.out.println("value "+i+"  "+lg[i]);
			 * 
			 * }
			 */

						
			String ruta;
			
//			Cursor partesCursor=partesDBAdapter.getRuta();				
//			startManagingCursor(partesCursor);
//			
//			if(partesCursor.moveToFirst()){
//				ruta= partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_VALOR));
//				if(ruta.equals("")) ruta= "/sdcard/";					
//			}
//			else ruta= "/sdcard/";
//			
			ruta= Environment.getExternalStorageDirectory() + ruta_parte;
			
			log = new Logs();					
			
			
			log.setLog(ruta, orden.getNumero(), "Solicitud",
					new_orden.getEstado(), new_orden.getSintoma(),
					new_orden.getModelo(), "", "");
			
			partesDBAdapter.close();

			//log.setLog(orden.getNumero(),"Solicitud","Se envia SMS solicitando partes",partesDBAdapter);

			
//			resumen.clearChoices();
//			SendSms sms = new SendSms(this);
//		    sms.sendSMS(new_orden);
			//resumen.clearChoices();
			SendSms sms = new SendSms(this);
		    sms.sendSMS(new_orden);

			Intent i = new Intent(this, Inicio.class);
			startActivity(i);
			
		   
			break;
		}
    }    
	
}